package com.hndzyu.yd.widget;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.RequestManager;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.hndzyu.yd.R;
import com.hndzyu.yd.bean.TX;
import com.hndzyu.yd.url.Path;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * 活动数据适配器
 */

public class JDViewAdapter {
    private List<TX.DataBean> mDatas;
    private RequestManager mContext;
    public JDViewListener mListener;

    public JDViewAdapter(RequestManager context, List<TX.DataBean> mDatas) {
        this.mContext = context;
        this.mDatas = mDatas;
    }

    /**
     * 获取数据的条数
     *
     * @return
     */
    public int getCount() {
        return mDatas == null ? 0 : mDatas.size();
    }

    /**
     * 获取摸个数据
     *
     * @param position
     * @return
     */
    public TX.DataBean getItem(int position) {
        return mDatas.get(position);
    }

    /**
     * 获取条目布局
     *
     * @param parent
     * @return
     */
    public View getView(JDAdverView parent) {
        return LayoutInflater.from(parent.getContext()).inflate(R.layout.lv_item_tx, null);
    }

    /**
     * 条目数据适配
     *
     * @param view
     * @param data
     */
    public void setItem(final int position, final View view, final TX.DataBean data) {
        ImageView iv=view.findViewById(R.id.iv_tx);
        TextView name = view.findViewById(R.id.tv_tx_name);
        TextView num = view.findViewById(R.id.tv_tx_num);
        TextView time=view.findViewById(R.id.tv_tx_time);
        if (iv!=null){ RequestOptions requestOptions = RequestOptions.circleCropTransform().error(R.mipmap.logo).diskCacheStrategy(DiskCacheStrategy.NONE);
        mContext.load(Path.img(data.getAvatar())).apply(requestOptions).into(iv);
        }
        name.setText(data.getNickname());
        num.setText("注册成功");
        time.setText(data.getTxtime());
        //你可以增加点击事件
//        view.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                mListener.onIntent(data, position);
//                /*Intent intent = new Intent(mContext,NoticeActivity.class);
//                intent.putExtra("jdata",data);
//                mContext.startActivity(intent);*/
//            }
//        });
    }

    interface JDViewListener {
        void onIntent(TX.DataBean item, int position);
    }

    /*
     * 根据时间戳来判断当前的时间是几天前,几分钟,刚刚
     * @param long_time
     * @return
     */
    public static String getTimeStateNew(String long_time) {
        String long_by_13 = "1000000000000";
        String long_by_10 = "1000000000";
        if (Long.valueOf(long_time) / Long.valueOf(long_by_13) < 1) {
            if (Long.valueOf(long_time) / Long.valueOf(long_by_10) >= 1) {
                long_time = long_time + "000";
            }
        }
        Timestamp time = new Timestamp(Long.valueOf(long_time));
        Timestamp now = new Timestamp(System.currentTimeMillis());
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//    System.out.println("传递过来的时间:"+format.format(time));
//    System.out.println("现在的时间:"+format.format(now));
        long day_conver = 1000 * 60 * 60 * 24;
        long hour_conver = 1000 * 60 * 60;
        long min_conver = 1000 * 60;
        long time_conver = now.getTime() - time.getTime();
        long temp_conver;
//    System.out.println("天数:"+time_conver/day_conver);
        if ((time_conver / day_conver) < 3) {
            temp_conver = time_conver / day_conver;
            if (temp_conver <= 2 && temp_conver >= 1) {
                return temp_conver + "天前";
            } else {
                temp_conver = (time_conver / hour_conver);
                if (temp_conver >= 1) {
                    return temp_conver + "小时前";
                } else {
                    temp_conver = (time_conver / min_conver);
                    if (temp_conver >= 1) {
                        return temp_conver + "分钟前";
                    } else {
                        return "刚刚";
                    }
                }
            }
        } else {
            return format.format(time);
        }
    }
}
